contents = $contents; } /** * Returns all IPs for the given hostname * * @param string $name * @return string[] */ public function getIpsForHost($name) { $name = \strtolower($name); $ips = array(); foreach (\preg_split('/\\r?\\n/', $this->contents) as $line) { $parts = \preg_split('/\\s+/', $line); $ip = \array_shift($parts); if ($parts && \array_search($name, $parts) !== \false) { // remove IPv6 zone ID (`fe80::1%lo0` => `fe80:1`) if (\strpos($ip, ':') !== \false && ($pos = \strpos($ip, '%')) !== \false) { $ip = \substr($ip, 0, $pos); } if (@\inet_pton($ip) !== \false) { $ips[] = $ip; } } } return $ips; } /** * Returns all hostnames for the given IPv4 or IPv6 address * * @param string $ip * @return string[] */ public function getHostsForIp($ip) { // check binary representation of IP to avoid string case and short notation $ip = @\inet_pton($ip); if ($ip === \false) { return array(); } $names = array(); foreach (\preg_split('/\\r?\\n/', $this->contents) as $line) { $parts = \preg_split('/\\s+/', $line, -1, \PREG_SPLIT_NO_EMPTY); $addr = (string) \array_shift($parts); // remove IPv6 zone ID (`fe80::1%lo0` => `fe80:1`) if (\strpos($addr, ':') !== \false && ($pos = \strpos($addr, '%')) !== \false) { $addr = \substr($addr, 0, $pos); } if (@\inet_pton($addr) === $ip) { foreach ($parts as $part) { $names[] = $part; } } } return $names; } }